package com.gdqy.FCS.web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdqy.FCS.common.api.FastResponse;
import com.gdqy.FCS.common.mvc.Args;
import com.gdqy.FCS.entity.Comment;
import com.gdqy.FCS.entity.User;
import com.gdqy.FCS.service.CommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("comment")
public class CommentController {
	
	
	@Autowired
	private CommentService commentService;
	
	//获取评论
	@RequestMapping("get_comment")
	public @ResponseBody Object get_comment(int object_id,String object_type,Args args,int pageindex, int pagesize){
		PageHelper.startPage(pageindex, pagesize);
		List<Comment> comments = commentService.selectByObj_idAndType(object_id, object_type);
		PageInfo<Comment> page = new PageInfo<Comment>(comments);
		return FastResponse._().errCode(0).subCode(0).errMsg("获取收藏作品成功")
				.args(args.getReqeustargs()).pageIndex(page.getPageNum())
				.pageSize(page.getSize()).total((int)page.getTotal()).pageCount(page.getPages())
				.hasNext(page.isHasNextPage()).list(comments);
	}
	
	//增加评论
	@ResponseBody
	@RequestMapping("add_comment")
	public  String add_comment(Comment comment,User user){
		String content =comment.getContent();
		String msg ="fail";
		if(content!=null && content!=""){
			comment.setDatetime(new Date());
			int data = commentService.insert(comment);
			if(data>0){
				msg="success";
				return msg;
			}
		}
		return msg;	
	}
}
